package com.triones.ljr.controller.web;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.triones.ljr.entity.dto.AdductDO;
import com.triones.ljr.entity.vo.AdductVO;
import com.triones.ljr.service.AdductSerivce;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 政策汇集
 *
 * @Author: triones
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/adduct")
public class AdductController extends BaseController {

    private final AdductSerivce adductSerivce;

    /**
     * 列表（分页）
     */
    @SaCheckPermission("system:adduct:pageList")
    @GetMapping("/pageList")
    public TableDataInfo<AdductVO> pageList(AdductDO dto, PageQuery pageQuery) {
        return adductSerivce.pageList(dto, pageQuery);
    }

    /**
     * 新增
     */
    @SaCheckPermission("system:adduct:add")
    @Log(title = "政策收集", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody AdductDO dto) {
        if (!adductSerivce.checkNameUnique(dto)) {
            return R.fail("新增上级名称'" + dto.getName() + "'失败，上级名称已存在");
        }
        return toAjax(adductSerivce.add(dto));
    }

    /**
     * 修改
     */
    @SaCheckPermission("system:adduct:edit")
    @Log(title = "政策收集", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody AdductDO dto) {
        if (!adductSerivce.checkNameUnique(dto)) {
            return R.fail("新增上级名称'" + dto.getName() + "'失败，上级名称已存在");
        }
        return toAjax(adductSerivce.edit(dto));
    }

    /**
     * 获取详情
     */
    @GetMapping("/{id}")
    public R<AdductVO> getInfo(@PathVariable Long id) {
        return R.ok(adductSerivce.getInfo(id));
    }

    /**
     * 删除
     */
    @SaCheckPermission("system:adduct:remove")
    @Log(title = "政策收集", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R<Void> remove(@PathVariable Long id) {
        return toAjax(adductSerivce.remove(id));
    }
}
